/***
*buff.h - methods related to working of the Buffer Manager
*
*
*Purpose:
*       This file contains prototypes for the methods that are used in 
*       the Buffer Manager
*
*       [Public]
*
****/

#ifndef BUFF_H
#define BUFF_H


#include <iostream>
#include <vector>
#include <deque>
#include <map>

//#include "adb_global.h"


using namespace std;

class Page
{
//private:
public:
    bool _is_pinned;
    bool _dirty_bit;
    int _priority;
    long _page_num;
	char data[1];

public:
    Page()
    {
        _is_pinned = 0;
        _dirty_bit = 0;
        _priority = -1;
        _page_num = -1;
        data[0] = '\0';
    }
};

class BMgr
{
public :
    long _memsize;
    long _pagesize;
    long _num_of_frames;
    Page *buffer_frames;
    deque<long> free_frames;
	deque<int>::iterator free_it;

    deque<long> used_frames;
	deque<int>::iterator used_it;

    vector<Page> mybuffer;
    map <long,long> pfdirectory;
    map <long,long>::iterator pfit;
public :
    BMgr()
    {}
    /*getters member functions*/
    long getmemsize();
    long getpagesize();
    long get_num_of_frames();

    /*setter member functions*/
    void setpagesize(int);
   
    /*remaining functions*/
    void display()
    {
        cout<<_memsize<<"  "<<_pagesize;
    }
   
    /* initialize the buffer according to the total size and the pagesize */            /*Done*/
    bool init_buffer(long _memSize, long _pageSize);

    /* Interface between Buffer Manager and Database Manager. As open_DB is done BM opens a file,
    fds get opened with the help of a Data Structures */
    int open_DB(char *_dbName);

    /* Will search through an array of dbIndexes and dbName , so instead of returning a fd ,
    will return the Index of the database.
    returns 0 if the database can not be closed.*/
    bool close_DB(char *_dbName);

    /* Creates space and returns to the Database Manager. When Database overflows then DB manager will ask
    the  Buffer Manager to increase the space */
    int createDB(char *_dbName, long _numOf_dbPage,  long _pageSize);
   
    int deleteDB(char *_dbName);

    int incrementDB(char *_dbName, long _numOf_dbPage, long _pageSize);

    Page* readPagetoFrame(char *_dbName, long _pageNum);

	Page* readPagetoFrame(long _pageNum);

	long Replacement_Policy();

    void findFreeFrame(long *);

    int writeFrametoPage(char *_dbName,long _frameNum);

    int commitFrame(char *_dbName, long _frameNum);

    int commitPage(char *_dbName, long _pageNum);

    int commitAll(char *_dbName);

    void showFrame(long framenum);        /*Done*/

    int releaseBuffer();

   
};

#endif